package com.ruoyi.util;

import com.ruoyi.basedocument.domain.BomAsscategory;
import com.ruoyi.basedocument.domain.BomCategory;
import com.ruoyi.basedocument.domain.BomCoefficient;
import com.ruoyi.basedocument.domain.BomMaterialhalf;
import com.ruoyi.basedocument.service.IBomAsscategoryService;
import com.ruoyi.basedocument.service.IBomCategoryService;
import com.ruoyi.basedocument.service.IBomCoefficientService;
import com.ruoyi.basedocument.service.IBomMaterialhalfService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class importRunnable implements Runnable{
    @Autowired
    private IBomAsscategoryService bomAsscategoryService;
    @Autowired
    private IBomCategoryService bomCategoryService;
    @Autowired
    private IBomMaterialhalfService bomMaterialhalfService;
    @Autowired
    private IBomCoefficientService bomCoefficientService;
    private List<BomAsscategory> bomAsscategoryList;
    private List<BomCoefficient> bomCoefficientList;
    private List<BomCategory> bomCategoryList;

    public List<BomAsscategory> getBomAsscategoryList() {
        return bomAsscategoryList;
    }

    public void setBomAsscategoryList(List<BomAsscategory> bomAsscategoryList) {
        this.bomAsscategoryList = bomAsscategoryList;
    }

    public List<BomCoefficient> getBomCoefficientList() {
        return bomCoefficientList;
    }

    public void setBomCoefficientList(List<BomCoefficient> bomCoefficientList) {
        this.bomCoefficientList = bomCoefficientList;
    }

    public List<BomCategory> getBomCategoryList() {
        return bomCategoryList;
    }

    public void setBomCategoryList(List<BomCategory> bomCategoryList) {
        this.bomCategoryList = bomCategoryList;
    }

    public void removeParam(){
        this.bomCategoryList = null;
        this.bomCoefficientList = null;
        this.bomAsscategoryList = null;
    }

    @Override
    public void run() {
        importData();
    }

    private void importData(){
        if(this.bomAsscategoryList!=null){
            List<BomAsscategory> list = this.bomAsscategoryList;
            for (int i = 0; i < list.size(); i++) {
                BomAsscategory bomAsscategory = list.get(i);
                if(StringUtils.isNotBlank(bomAsscategory.getMaterialbomnumber())&& StringUtils.isNotBlank(bomAsscategory.getCategorynumber())){
                    BomCategory bomCategory = new BomCategory();
                    bomCategory.setNumber(bomAsscategory.getCategorynumber());
                    List<BomCategory> bomCategories = bomCategoryService.selectBomCategoryList2(bomCategory);
                    if(bomCategories==null||bomCategories.size()==0){
                        continue;
                    }
                    BomMaterialhalf bomMaterialhalf = new BomMaterialhalf();
                    bomMaterialhalf.setCfmaterialnumber(bomAsscategory.getMaterialbomnumber());
                    List<BomMaterialhalf> bomMaterialhalves = bomMaterialhalfService.selectBomMaterialhalfList2(bomMaterialhalf);
                    if(bomMaterialhalves==null||bomMaterialhalves.size()==0){
                        continue;
                    }
                    BomAsscategory bomAsscategory1 = new BomAsscategory();
                    bomAsscategory1.setMaterialbomnumber(bomAsscategory.getMaterialbomnumber());
                    List<BomAsscategory> bomAsscategories = bomAsscategoryService.selectBomAsscategoryList2(bomAsscategory1);
                    if(bomAsscategories!=null&&bomAsscategories.size()!=0){
                        for (int i1 = 0; i1 < bomAsscategories.size(); i1++) {
                            bomAsscategoryService.deleteBomAsscategoryById(bomAsscategories.get(i1).getId());
                        }
                    }
                    bomAsscategoryService.insertBomAsscategory(bomAsscategory);
                }
            }
        }
        if(this.bomCoefficientList!=null){
            List<BomCoefficient> list = this.bomCoefficientList;
            for (int i = 0; i < list.size(); i++) {
                BomCoefficient bomCoefficient = list.get(i);
                bomCoefficientService.insertBomCoefficient(bomCoefficient);
            }
        }
        if(this.bomCategoryList!=null){
            List<BomCategory> list = this.bomCategoryList;
            for (int i = 0; i < list.size(); i++) {
                BomCategory bomCategory = list.get(i);
                BomCategory bomCategory1 = new BomCategory();
                bomCategory1.setNumber(bomCategory.getNumber());
                List<BomCategory> bomCategories = bomCategoryService.selectBomCategoryList2(bomCategory1);
                if(bomCategories!=null&&bomCategories.size()!=0){
                    for (int i1 = 0; i1 < bomCategories.size(); i1++) {
                        bomCategoryService.deleteBomCategoryById(bomCategories.get(i1).getId());
                    }
                }
                bomCategoryService.insertBomCategory(bomCategory);
            }
        }
    }
}
